package com.tk.exam.service.impl;

import com.github.pagehelper.PageInfo;
import com.tk.exam.dao.ClassDao;
import com.tk.exam.domain.*;
import com.tk.exam.exception.ApiException;
import com.tk.exam.mapper.TkClassMapper;
import com.tk.exam.mapper.TkExamInfoExtMapper;
import com.tk.exam.mapper.TkStudentMapper;
import com.tk.exam.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author: xujg
 * @Description: TODO
 * @date: 2019-06-17 17:05
 */
@Service
@Transactional
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassDao classDao;
    @Autowired
    private TkClassMapper classMapper;
    @Autowired
    private TkExamInfoExtMapper examInfoMapper;
    @Autowired
    private TkStudentMapper studentMapper;


    @Override
    public PageInfo<TkClass> getListWithPage() {
        return classDao.getListWithPage();

    }

    @Override
    public List<TkClass> getList() {
        TkClassExample classExample = new TkClassExample();
        classExample.setOrderByClause("create_time DESC");
        return this.classMapper.selectByExample(classExample);
    }

    @Override
    public int editClass(TkClass tkClass) {
        int i =0;
        Integer id = tkClass.getId();
        TkClass c = classMapper.selectByPrimaryKey(id);
        if(c==null){
            throw ApiException.of("该班级不存在");
        }
        c.setClassDesc(tkClass.getClassDesc());
        c.setClassName(tkClass.getClassName());
        c.setUpdateTime(new Date());
        i = classMapper.updateByPrimaryKey(c);
        return i;
    }

    @Override
    public int addClass(TkClass tkClass) {
        int i = 0;
        TkClass c = new TkClass();
        c.setClassName(tkClass.getClassName());
        c.setClassDesc(tkClass.getClassDesc());
        c.setCreateTime(new Date());
        c.setUpdateTime(new Date());
        i = classMapper.insert(c);
        return i;
    }

    @Override
    public int deleteClass(String[] classIds) {
        int i =0;
        for (String classId :classIds ) {
            TkClass c = classMapper.selectByPrimaryKey(Integer.parseInt(classId));
            if(c==null){
                throw ApiException.of("该班级不存在");
            }
            TkExamInfoExample example = new TkExamInfoExample();
            example.createCriteria().andClassIdEqualTo(Integer.parseInt(classId));
            List<TkExamInfo> infos = examInfoMapper.selectByExample(example);
            if(!CollectionUtils.isEmpty(infos)){
                throw ApiException.of("该项目已被引用过，不能被删除！");
            }

        }

        for (String classId : classIds) {
            classMapper.deleteByPrimaryKey(Integer.parseInt(classId));
            //级联删除考生
            TkStudentExample sExample = new TkStudentExample();
            sExample.createCriteria().andClassIdEqualTo(Integer.parseInt(classId));
            List<TkStudent> students = studentMapper.selectByExample(sExample);
            if(!CollectionUtils.isEmpty(students)){
                for (TkStudent student : students) {
                    studentMapper.deleteByPrimaryKey(student.getId());
                }
            }
        }
        i =1;
        return i;
    }
}
